package com.xinghen.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author xinghen
 * @ClassName: ThreadPoolConfig
 * @Package: com.xinghen.core
 * @Description TODO
 * @create 2017-12-07 16:20
 */
@Configuration
public class ThreadPoolConfig {

    @Autowired
    private ApplicationConfig applicationConfig;

    /**
     * 默认线程池
     *
     * @return
     */
    @Bean
    public Executor defaultThreadPool() {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        // 设置线程池
        executor.setPoolSize(applicationConfig.getThreadPool().getPoolSize());
        executor.setThreadPriority(applicationConfig.getThreadPool().getThreadPriority());
        executor.setThreadNamePrefix(applicationConfig.getThreadPool().getThreadNamePrefix());
        // 拒接策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

}
